<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: Weather.php 1183 2011-06-07 10:34:42Z chen.qiao $$
 * @copyright   Copyright (c) 2011 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Weather class
 * 
 * access to google weather api get the weather condition xml and decode it array
 * 
 * @docinfo
 * 
 * @package     Application
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: Weather.php 1183 2011-06-07 10:34:42Z chen.qiao $$
 */

class Evebit_Weather {
	
	/**
	 * @var $_apiUrl string
	 */
	private $_apiUrl;
	/**
	 * @var $_cachePath string
	 */
	private $_cachePath;
	/**
	 * @var $_hostUrl string
	 */
	private $_hostUrl;
	
	/**
	 * __construct of the class
	 */
	public function __construct() {
		$this->_hostUrl = 'http://www.google.com';
		$this->_apiUrl = 'http://www.google.com/ig/api?hl=en&weather=';
		$this->_cachePath = RP . '/var/cache/weather/';
	}
	
	/**
	 * get weather xml with api url and decode it
	 * 
	 * @param string $city
	 * @return array
	 */
	public function getWeather($city) {
		$weatherArr = array();
		$city = ucwords($city);
		$fileName = date('Ymd', strtotime('now')) . $city . '.xml';
		$weatherArr = $this->cacheXml($city, $fileName);
		return $weatherArr;
	}
	
	/**
	 * decode the weather xml
	 * 
	 * @param string $cacheXml
	 * @return array|string
	 */
	private function decodeXml($cacheXml) {
		$result = '';
		$dom = new DOMDocument();
		if (!$dom->load($cacheXml)) {
			$result = 'Weather data failed to load, please refresh the page to try again';
		}
		if (!$result) {
			$currentDom = $dom->getElementsByTagName('current_conditions');
			$current = array(
					'condition'=> 'condition', 
					'tempF'=> 'temp_f', 
					'tempC'=> 'temp_c', 
					'humidity'=> 'humidity', 
					'image'=> 'icon');
			$result['current'] = $this->getWeatherArray($currentDom, $current);
			$forecastDom = $dom->getElementsByTagName('forecast_conditions');
			$forecast = array(
					'week'=> 'day_of_week', 
					'low'=> 'low', 
					'high'=> 'high', 
					'image'=> 'icon', 
					'condition'=> 'condition');
			$result['forecast'] = $this->getWeatherArray($forecastDom, $forecast);
		}
		return $result;
	}
	
	/**
	 * get the array with the weather dom
	 * 
	 * @param object $weatherDom
	 * @param array $xmlArr
	 * @return array
	 */
	private function getWeatherArray($weatherDom, $xmlArr) {
		$weatherArr = array();
		foreach ($weatherDom as $k => $weather) {
			foreach ($xmlArr as $key => $value) {
				$weatherAttribute = $weather->getElementsByTagName($value);
				$weatherArr[$k][$key] = $weatherAttribute->item(0)->attributes->item(0)->nodeValue;
				if ($key == 'tempF') {
					$weatherArr[$k][$key] .= '°F';
				} else if ($key == 'tempC') {
					$weatherArr[$k][$key] .= '°C';
				} else if ($key == 'image') {
					$weatherArr[$k][$key] = $this->_hostUrl . $weatherArr[$k][$key];
				} else if ($key == 'low' || $key == 'high') {
					$weatherArr[$k][$key] = round(($weatherArr[$k][$key] - 32) / 1.8) . '°C';
				}
			}
		}
		return $weatherArr;
	}
	
	/**
	 * get weather xml with api url and decode it
	 * 
	 * @param string $city
	 * @param string $fileName
	 * @return array|string
	 */
	private function cacheXml($city, $fileName) {
		$message = '';
		$flag = true;
		$i = 0;
		if (!is_dir($this->_cachePath)) {
			mkdir($this->_cachePath);
		}
		$cachePath = chop($this->_cachePath);
		if ($cachePath != '') {
			if (substr($cachePath, strlen($cachePath) - 1, strlen($cachePath)) != '/') {
				$cachePath .= '/';
			}
		}
		$cachePath .= $fileName;
		$url = $this->_apiUrl . $city;
		if (!file_exists($cachePath) || filesize($cachePath) == 0) {
			while (!$message && $i < 20) {
				$i++;
				$message = @file_get_contents($url);
			}
			if ($message) {
				file_put_contents($cachePath, $message);
			} else {
				$result = 'Weather data failed to load, please refresh the page to try again';
				$flag = false;
			}
		}
		if ($flag) {
			$result = $this->decodeXml($cachePath);
		}
		return $result;
	}

}